﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DAL;
using Model;
using BLL;
public partial class Index : System.Web.UI.Page
{
    private  string strSql;

    public  string StrSql
    {
        get { return strSql; }
        set { strSql = value; }
    }
    private static string strArea;

    public static string StrArea
    {
        get { return strArea; }
        set { strArea = value; }
    }
    private static string strPrice;

    public static string StrPrice
    {
        get { return strPrice; }
        set { strPrice = value; }
    }
    private static string strLeaseMode;

    public static string StrLeaseMode
    {
        get { return strLeaseMode; }
        set { strLeaseMode = value; }
    }
    private static string strStandard;

    public static string StrStandard
    {
        get { return strStandard; }
        set { strStandard = value; }
    }

    private static string strRoomNumber;

    public static string StrRoomNumber
    {
        get { return Index.strRoomNumber; }
        set { Index.strRoomNumber = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {

            HouseDataBind();
            HouseAreaBind();
            HouseLeaseModeBind();
        }
    }
    public void HouseLeaseModeBind() {

        List<LeaseMode> listArea = LeaseModeManage.GetAllLeaseModes();
        dl_LeaseMode.DataSource = listArea;
        dl_LeaseMode.DataBind();
    }

    public void HouseAreaBind() {

        List<Area> listArea = AreaManage.GetAllAreas();
        dl_Area.DataSource = listArea;
        dl_Area.DataBind();
    }
    public void HouseDataBind() {

        if(StrArea!=null){

            StrSql += " and  AreaId = " + StrArea + " ";

        }
        if (StrLeaseMode!=null) {

            StrSql += " and LeaseModeId = " + StrLeaseMode + "  ";
        }
        if (StrPrice != null) {

            StrSql += StrPrice;
        }
        if (StrRoomNumber!=null)
        {
            StrSql += StrRoomNumber;
        }
        int a = AspNetPager1.StartRecordIndex;//开始记录数
        int c = AspNetPager1.PageSize;//每页显示多少条

        DataTable dtHouse =HouseService.getTopHouseByTime(a, c,StrSql);
        DataTable result=new DataTable();
        DataColumn column01 = new DataColumn("Id");
        DataColumn column02 = new DataColumn("Name");
        DataColumn column03 = new DataColumn("ExpirationDate");
        DataColumn column04 = new DataColumn("ThumbnailUri");
        DataColumn column05 = new DataColumn("Price");
        DataColumn column06 = new DataColumn("Address");
        DataColumn column07 = new DataColumn("Standard");
        DataColumn column08 = new DataColumn("PublishTime");
        DataColumn column09 = new DataColumn("InTakeTime");
        result.Columns.Add(column01);
        result.Columns.Add(column02);
        result.Columns.Add(column03);
        result.Columns.Add(column04);
        result.Columns.Add(column05);
        result.Columns.Add(column06);
        result.Columns.Add(column07);
        result.Columns.Add(column08);
        result.Columns.Add(column09);
       
        foreach (DataRow item in dtHouse.Rows)
        {
            DataRow row=result.NewRow();
            row["Id"]=Convert.ToString(item[0]);
            row["Name"] = Convert.ToString(item[21]);
            row["ExpirationDate"] = Convert.ToString(item[8]);
            row["ThumbnailUri"] = Convert.ToString(item[18]);
            row["Price"] = Convert.ToString(item[13]);
            row["Address"] = Convert.ToString(item[5]);
            row["Standard"] = Convert.ToString(item[16]);
            row["PublishTime"] = DateTime.Parse(Convert.ToString(item[14])).ToString("yyyy/MM/dd");
            row["InTakeTime"] = DateTime.Parse(Convert.ToString(item[9])).ToString("yyyy/MM/dd");
            result.Rows.Add(row);
        }
        this.dl_HouseData.DataSource = result;
        AspNetPager1.RecordCount = HouseService.getHouseNum(StrSql);
        this.dl_HouseData.DataBind();
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {

        HouseDataBind();
    
    }
    
    protected void HouseDetail_Click(object sender, EventArgs e)
    {

    }
    protected void dl_HouseData_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "showDetail") {


            Response.Redirect("~/HouseDetail.aspx?HouseId=" + Convert.ToString(e.CommandArgument) + "");

        }
    }


    protected void dl_Area_ItemCommand(object source, DataListCommandEventArgs e)
    {
        StrArea = e.CommandArgument.ToString();
        HouseDataBind();
    }
    protected void dl_LeaseMode_ItemCommand(object source, DataListCommandEventArgs e)
    {
        StrLeaseMode = e.CommandArgument.ToString();
        HouseDataBind();
    }
    protected void OnClick_Price(object sender, EventArgs e)
    {
        LinkButton lbtn = (LinkButton)sender;
        int  priceId=Convert.ToInt32(lbtn.Attributes["PriceId"]);
        switch (priceId)
        {
            case 1:
                StrPrice = " and Price < 500";
                break;
            case 2:
                StrPrice = " and Price between 500 and 1000";
                break;
            case 3:
                StrPrice = " and Price between 1000 and 1500";
                break;
            case 4:
                StrPrice = " and Price between 1500 and 2000";
                break;
            case 5:
                StrPrice = " and Price between 2000 and 3000";
                break;
            case 6:
                StrPrice = " and Price between 3000 and 4500";
                break;
            case 7:
                StrPrice = " and Price > 4500";
                break;
            default:
                StrPrice = null;
                break;
        }
        HouseDataBind();
    }
    protected void OnClick_RoomNumber(object sender, EventArgs e)
    {
        LinkButton lbtn = (LinkButton)sender;
        int RoomNumber=Convert.ToInt32(lbtn.Attributes["RoomNumber"]);
        switch (RoomNumber)
        {
            case 1:
                StrRoomNumber = " and RoomNumber = 1 ";
                break;
            case 2:
                StrRoomNumber = " and RoomNumber = 2 ";
                break;
            case 3:
                StrRoomNumber = " and RoomNumber = 3 ";
                break;
            case 4:
                StrRoomNumber = " and RoomNumber = 4 ";
                break;
            default:
                StrRoomNumber = null;
                break;
        }
        HouseDataBind();
    }
    protected void OnClick_Address_Unlimit(object sender, EventArgs e)
    {
        StrArea = null;
        HouseDataBind();
    }
    protected void OnClick_LeaseModeId_Unlimit(object sender, EventArgs e)
    {
        StrLeaseMode = null;
        HouseDataBind();
    }
}